<template>
  <div class="flight-item">
    <!-- 显示的机票信息 -->
    <div @click="isShow=!isShow">
      <!-- 显示的机票信息 -->
      <el-row type="flex" align="middle" class="flight-info">
        <el-col :span="6">
          <span>{{flightsData.airline_name}}</span>
          {{flightsData.flight_no}}
        </el-col>
        <el-col :span="12">
          <el-row type="flex" justify="space-between" class="flight-info-center">
            <el-col :span="8" class="flight-airport">
              <strong>{{flightsData.dep_time}}</strong>
              <span>{{flightsData.org_airport_name}}</span>
            </el-col>
            <el-col :span="8" class="flight-time">
              <!-- 乘坐时长 -->
              <span>{{getDiff}}</span>
            </el-col>
            <el-col :span="8" class="flight-airport">
              <strong>{{flightsData.arr_time}}</strong>
              <span>{{flightsData.dst_airport_name}}</span>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="6" class="flight-info-right">
          ￥
          <span class="sell-price">{{flightsData.base_price / 2}}</span>起
        </el-col>
      </el-row>
    </div>
    <!-- 低价推荐 -->
    <div class="flight-recommend" v-if="isShow">
      <!-- 隐藏的座位信息列表 -->
      <el-row type="flex" justify="space-between" align="middle">
        <el-col :span="4">低价推荐</el-col>
        <el-col :span="20">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            class="flight-sell"
            v-for="(item,index) in flightsData.seat_infos"
            :key="index"
          >
            <el-col :span="16" class="flight-sell-left">
              <span>{{item.name}}</span>
              | {{item.supplierName}}
            </el-col>
            <el-col :span="5" class="price">￥{{item.par_price}}</el-col>
            <el-col :span="3" class="choose-button">
              <el-button type="warning" size="mini" @click="jumpOrder(item)">选定</el-button>
              <p>剩余：{{item.discount}}</p>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
export default {
  props: {   //通过父传子的方式渲染机票列表信息
    flightsData: {
      required: true,
      type: Object,
      // 设置默认值  
      //   default: {}       //报错： Invalid default value for prop "flightsdata": Props with type Object/Array must use a factory function to return the default value.
      // j解决：如果prop属性是对象或者数组类型，那么默认值的设置必须通过一个函数来设置
      default: () => { return {} }
    }
  },
  data() {
    return {
      isShow: false
    }
  },
  // 计算乘坐时长
  computed: {
    // 定义计算属性
    // 计算属性的特点
    // 1.看起来像函数，使用起来是一个属性,所以调用的时候不要加()
    // 2.需要人为调用
    // 3.一般会有返回值
    // 4.不能响应异步操作中的数据变化
    getDiff() {
      // 思路：乘坐时长=到达时间-出发时间--以分钟为单位，先全部转化为分钟
      // 1.出发时间的时和分
      let cfsf = this.flightsData.dep_time.split(':')  //["20", "30"]
      // 2.到达时间的时和分
      let ddsf = this.flightsData.arr_time.split(':')

      //3. 计算两者的时间差
      let cffz = cfsf[0] * 60 + +cfsf[1]  //出发总分钟，将出发时间从转换为分钟
      // console.log(cffz);   //不能直接拼接 记得要转换为数值

      let ddfz = ddsf[0] * 60 + (ddsf[1] - 0)  //到达总分钟
      let diff = ddfz - cffz //两者时间差 （分钟为单位）
      // 出现结果 -22小时-45分；0点后需要进行 +24小时处理
      if (diff < 0) {
        diff = diff + 24 * 60
      }
      return `${Math.floor(diff / 60)}小时${diff % 60}分`
    }
  },
  methods: {
    // 点击选定，跳转订单页
    jumpOrder(item) {
      console.log(this.flightsData);
      this.$router.push(`/air/order?id=${this.flightsData.id}&seat_xid=${item.seat_xid}`)
    }
  }
}
</script>


<style scoped lang="less">
.flight-item {
  border: 1px #ddd solid;
  margin-bottom: 10px;

  .flight-info {
    padding: 15px;
    cursor: pointer;

    > div {
      &:first-child,
      &:last-child {
        text-align: center;
      }
    }
  }

  .flight-info-center {
    padding: 0 30px;
    text-align: center;

    .flight-airport {
      strong {
        display: block;
        font-size: 24px;
        font-weight: normal;
      }
      span {
        font-size: 12px;
        color: #999;
      }
    }

    .flight-time {
      span {
        display: inline-block;
        padding: 10px 0;
        border-bottom: 1px #eee solid;
        color: #999;
      }
    }
  }

  .flight-info-right {
    .sell-price {
      font-size: 24px;
      color: orange;
      margin: 0 2px;
    }
  }
}

.flight-recommend {
  background: #f6f6f6;
  border-top: 1px #eee solid;
  padding: 0 20px;

  .flight-sell {
    border-bottom: 1px #eee solid;
    padding: 10px 0;

    &:last-child {
      border-bottom: none;
    }

    .flight-sell-left {
      font-size: 12px;
      span {
        color: green;
      }
    }

    .price {
      font-size: 20px;
      color: orange;
    }

    .choose-button {
      text-align: center;
      color: #666;
      button {
        display: block;
        width: 100%;
        margin-bottom: 5px;
      }
    }
  }
}
</style>